# tu-trabajo-ya
# Platform: Windows 64-bits
# Dependencies: Ruby, Gem, Chrome Webdriver
# Gems List: watir, watir-webdriver
#
# OS    Expected Location of Chrome
# Linux  /usr/bin/google-chrome
# Mac    /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome
# Windows XP     %HOMEPATH%\Local Settings\Application Data\Google\Chrome\Application\chrome.exe
# Windows Vista  C:\Users\%USERNAME%\AppData\Local\Google\Chrome\Application\chrome.exe

puts "Detecting your Operative System:" + RUBY_PLATFORM


#settings
$host = "http://desarrollo-local.tu-trabajo-ya.com/"

$cred = {
    "user" => "demo",
    "email" => "demo@demo.com",
    "pwd" => "123456"
}

$browserUTAddTime = {
        "ff" => 0,
        "chrome" => 0,
        "ie" => 3,
        "ieChangeMode" => 8,
        "ie9" => "IE9",
        "ie8" => "IE8",
        "ie7" => "IE7"
}

# Selecting browser
puts "Please select a Browser (ff, ie, chrome):"
$browserSelect = STDIN.gets.split("\n")[0].to_s
# FPO -
# $browserSelect = "chrome"
puts "[info] Starting tests with "+$browserSelect

# Windows
if RUBY_PLATFORM =~ /mswin32/ || RUBY_PLATFORM =~ /mingw/ then
        if $browserSelect == 'ie' then
                require 'watir'
                $browser = Watir::Browser.new
                puts "[info] Now you have " + $browserUTAddTime["ieChangeMode"].to_s + " seconds to change the mode"
                sleep $browserUTAddTime[$browserSelect] + $browserUTAddTime["ieChangeMode"]
        else
                require 'watir-webdriver'

                if $browserSelect == 'chrome' then
                        system('start /d "C:\\Users\\%USERNAME%\\AppData\\Local\\Google\\Chrome\\Application\\" chromedriver.exe')
                        $browser = Watir::Browser.new(:remote, :url => "http://localhost:9515")
                else
                        $browser = Watir::Browser.new($browserSelect)
                end
        end
end

# Linux
if RUBY_PLATFORM =~ /linux/ then
        require "rubygems"
        require "watir-webdriver"
        if $browserSelect == 'chrome' then
                $browser = Watir::Browser.new :chrome
        end

        if $browserSelect == 'ff' then
                $browser = Watir::Browser.new :ff
        end
end

# Mac
if RUBY_PLATFORM =~ /darwin/ then
        puts "Configuration for Mac is missing, please modify the script"
end

#
# Test Cases
#
def go webPage
    $browser.goto webPage
end

def testHomePage
    go $host
end

def testLogin
    $browser.goto $host+""
	$browser.link(:text, "Empresas").click
    $browser.text_field(:id, "usuario").set($cred["user"])
    $browser.text_field(:id, "password").set($cred["pwd"])
    $browser.button(:id, "Ingresar").click

        if $browser.link(:text, "Salir").exists? then
                puts "[ok] Login Successful on " + $host
        else
                puts "[fail] Login Failed on " + $host
        end
end

def testLogout
    $browser.link(:text, "Salir").click

        if $browser.link(:text, "Salir").exists? then
                puts "[fail] Logout Failed on " + $host
        else
                puts "[ok] Logout Successful on " + $host
        end
end

def closeBrowser
    puts "press a key to close the browser"
    STDIN.gets
    $browser.close
end


#Tests to execute
testLogin
testLogout
closeBrowser
